/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.layer
{

import flash.events.Event;

import mx.styles.IStyleClient;

import org.teotigraphix.dx.event.BehaviorLayerEvent;
import org.teotigraphix.dx.interfaces.IBehaviorLayer;
import org.teotigraphix.dx.interfaces.IComponentAware;
import org.teotigraphix.dx.interfaces.IComponentBehavior;
import org.teotigraphix.dx.interfaces.IComponentLayerClient;
import org.teotigraphix.dx.patterns.manager.StyleBindingManager;

/**
 * The default <code>IBehaviorLayer</code> implementation of an 
 * <code>IBehaviorLayerAware</code> Component.
 * 
 * <p>The behavior layer abstracts the runtime behavior from a Component.
 * An example of this would be if the Component listened to mouse clicks
 * to determine a specific state. In this situation you would abstract the
 * control functionality into the core behavior of the Component. If the
 * operations are decoupled enough, a subclass of <code>ComponentBehavior</code>
 * could decouple the functionality to a point of code reuse in another
 * Component.</p>
 * 
 * <p>The layer allows for registration, removal, testing of an api and
 * retrieval of one or all of the layer's currently registered apis.</p>
 * 
 * <p>The layer also will dispatch the <code>BehaviorLayerEvent.BEHAVIOR_ADD</code>
 * and <code>BehaviorLayerEvent.BEHAVIOR_REMOVE</code> events when an 
 * <code>IComponentBehavior</code> is registered and removed. The behavior layer 
 * will also dispatch these events when the layer's <code>activate()</code> and
 * <code>deactivate()</code> methods are called.</p>
 * 
 * <p>When a behavior layer is activated, this call will usually come from a call
 * to the <code>IComponentBehaviorLayerAware</code>'s host Component's <code>behaviorLayer</code>
 * setter.</p>
 * 
 * <p>When a behavior layer is deactivated, this call will usually come from a call
 * to the <code>IComponentBehaviorLayerAware</code>'s host Component's <code>behaviorLayer</code>
 * setter.</p>
 * 
 * <p>When a behavior layer's <code>component</code> property is set, the layer will
 * run through all the currently registered <code>IComponentBehavior</code>s and
 * reset their <code>component</code> property as well.</p>
 * 
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 07-07-09
 * 
 * @see org.teotigraphix.dx.event.BehaviorLayerEvent
 * @see org.teotigraphix.dx.patterns.manager.StyleBindingManager
 * @see org.teotigraphix.dx.interfaces.IComponentLayerClient
 * 
 * @example 
 * 
 * <listing version="3.0">
 * // register
 * myComponent.behaviorLayer.registerBehavior(ITitleBar, new TitleLabelBehavior());
 * // remove
 * var behavior:IComponentBehavior = myComponent.behaviorLayer.removeBehavior(ITitleBar);
 * // test
 * if (!myComponent.behaviorLayer.hasBehavior(ITitleBar))
 * {
 *     myComponent.behaviorLayer.registerBehavior(ITitleBar, new TitleLabelBehavior());
 * }
 * // return
 * var behavior:IComponentBehavior = myComponent.behaviorLayer.getBehavior(ITitleBar);
 * ITitleBar(behavior.titleLabel).text = "New title";
 * </listing>
 */
public class BehaviorLayer 
	   extends ComponentLayer 
	   implements IBehaviorLayer
{
	include "../../core/Version.as"
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function BehaviorLayer()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  IComponentBehaviorLayer API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorLayer#registerBehavior()
	 */
	public function registerBehavior(api:Class, skin:IComponentBehavior):void
	{
		super.registerProxy(api, skin);
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorLayer#removeBehavior()
	 */
	public function removeBehavior(api:Class):IComponentBehavior
	{
		return super.removeProxy(api) as IComponentBehavior;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorLayer#hasBehavior()
	 */
	public function hasBehavior(api:Class):Boolean
	{
		return super.hasProxy(api);;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorLayer#getBehavior()
	 */
	public function getBehavior(api:Class):IComponentBehavior
	{
		return super.getProxy(api) as IComponentBehavior;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorLayer#getBehaviors()
	 */
	public function getBehaviors():Array
	{
		var proxies:Array = [];
		var items:Array = getAllProxies();
		var len:int = items.length;
		for (var i:int = 0; i < len; i++)
		{
			proxies.push(items[i]);
		}
		return proxies;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Sets the registered <code>IComponentBehavior</code>s <code>component</code>
	 * property to the new <code>component</code> value.
	 * 
	 * <p>This method will also update each <code>IComponentBehavior</code>
	 * with the <code>ToolKitStyleManager.initialize()</code>.</p>
	 */
	override protected function commitComponent():void
	{
		super.commitComponent();
		
		var proxies:Array = getAllProxies();
		var len:int = proxies.length;
		for (var i:int = 0; i < len; i++)
		{
			var proxy:IComponentAware = proxies[i];
			if (component)
			{
				StyleBindingManager.initialize(this, IStyleClient(component));
				
				// don't set again if already component
				// lock in component handlers and aliases, if skin is not
				// created on component, parts and skin handlers will not
				// be added until the skin is added to the Component's display
				if (proxy.component !== component)
					proxy.component = component;				
			}
			else
			{
				// clear out all listeners and refs from the behavior that
				// are connected to the Component
				proxy.component = null;
			}
		}
	}
	
	/**
	 * Creates and returns a <code>BehaviorLayerEvent</code> instance.
	 * 
	 * @inheritDoc 
	 * @uses org.teotigraphix.dx.event.BehaviorLayerEvent
	 */
	override protected function createLayerEvent(
		type:String, load:Boolean = false):Event
	{
		return new BehaviorLayerEvent(type);
	}
	
	/**
	 * @inheritDoc
	 * @event org.teotigraphix.dx.event.BehaviorLayerEvent.BEHAVIOR_ADD
	 * @uses org.teotigraphix.dx.core.ToolKitLayer#createLayerEvent()
	 */
	override protected function dispatchAddEvent(api:Class, proxy:IComponentLayerClient):void
	{
		var e:BehaviorLayerEvent = 
			createLayerEvent(BehaviorLayerEvent.BEHAVIOR_ADD) as BehaviorLayerEvent;
		
		e.api = api;
		e.behavior = proxy as IComponentBehavior;
		e.component = component;
		
		dispatchEvent(e);
		// TODO (mschmalle) see if this is correct
		if (component)
			component.dispatchEvent(e);
	}
	
	/**
	 * @inheritDoc
	 * @event org.teotigraphix.dx.event.BehaviorLayerEvent.BEHAVIOR_REMOVE
	 * @uses org.teotigraphix.dx.core.ToolKitLayer#createLayerEvent()
	 */
	override protected function dispatchRemoveEvent(api:Class, proxy:IComponentLayerClient):void
	{
		var e:BehaviorLayerEvent = 
			createLayerEvent(BehaviorLayerEvent.BEHAVIOR_REMOVE) as BehaviorLayerEvent;
		
		e.api = api;
		e.behavior = proxy as IComponentBehavior;
		e.component = component;
		
		dispatchEvent(e);
		// TODO (mschmalle) see if this is correct
		if (component)
			component.dispatchEvent(e);
	}	
}
}